package com.ekingstar.framework.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ekingstar.framework.domain.PublicCode;
import com.ekingstar.framework.service.CodeException;
import com.ekingstar.framework.service.CodeService;

public class CodeUtil {
	private static CodeUtil codeUtil;
	private static CodeService codeService;

	public Map getNameCodes(String tableName){
		return codeService.getNameCodes(tableName);
	}
	public String getNameOrCodeValueRequire(String tableName, String fromColumnName,
			String toColumnName, String value)  {
		String returnValue = value;
		try {
			if(!Utility.isEmpty(returnValue))
			{
				returnValue = codeService.getCodeFromName(tableName, fromColumnName,
						toColumnName, value);
			}
		} catch (CodeException e) {
			e.printStackTrace();
			
		}
		return returnValue;
	}
	public String getNameOrCodeValue(String tableName, String fromColumnName,
			String toColumnName, String value) throws CodeException {
		String returnValue = value;
		try {
			if(!Utility.isEmpty(returnValue))
			{
				returnValue = codeService.getCodeFromName(tableName, fromColumnName,
						toColumnName, value);
			}
		} catch (CodeException e) {
			e.printStackTrace();
			throw new CodeException("获取数据失败!");
		}
		return returnValue;
	}
	
	public String getCodeValueIfExist(String tableName, String fromColumnName,
			String toColumnName, String value) throws CodeException {
		String returnValue = value;
		try {
			if(!Utility.isEmpty(returnValue))
			{
				returnValue = codeService.getCodeFromNameIfExist(tableName, fromColumnName,
						toColumnName, value);
			}
		} catch (CodeException e) {
			e.printStackTrace();
			throw new CodeException("获取数据失败!");
		}
		return returnValue;
	}
	
	public List<PublicCode> getCodePulbicList(String tableName, String key,String value) throws CodeException
	{
		return codeService.getPulbicList(tableName, key, value);
	}
	public List<PublicCode> getCodePulbicList(String tableName, String key,String value,String param) throws CodeException
	{
		return codeService.getPulbicList(tableName, key, value,param);
	}
	public List<PublicCode> getCodePulbicList(String sql) throws CodeException
	{
		List<PublicCode> list=new ArrayList();
		try
		{
			list=codeService.getPulbicList(sql);
		}
		catch(CodeException ex)
		{
		throw new CodeException("sql拼写失败，请按照要求拼写");
		}
		return list;
		
	}
	public List<PublicCode> getCodePulbicListForCombobox(String tableName, String key,String value) throws CodeException
	{
		return codeService.getPulbicListForComboBox(tableName, key, value);
	}
	public List<PublicCode> getCodePulbicListForCombobox(String tableName, String key,String value,String param) throws CodeException
	{
		return codeService.getPulbicListForComboBox(tableName, key, value,param);
	}
	/**
	 * 取用户类别
	 * @return
	 * @throws CodeException
	 */
	public List<PublicCode> getUserType() throws CodeException
	{
		return codeService.getUserType();
	}
	
	public List<PublicCode> getCodePulbicListForMoreSelect(String tableName, String key,String value,String param) throws CodeException
	{
		return codeService.getPulbicListForMoreSelect(tableName, key, value,param);
	}
	public List<PublicCode> getCodePulbicListForCombobox(String sql) throws CodeException
	{
		List<PublicCode> list=new ArrayList();
		try
		{
			list=codeService.getPulbicListForComboBox(sql);
		}
		catch(CodeException ex)
		{
		throw new CodeException("sql拼写失败，请按照要求拼写");
		}
		return list;
	}

	public static CodeUtil getInctance() {
		if (Utility.isEmpty(codeUtil)) {
			codeUtil = new CodeUtil();
			codeService=(CodeService)ServiceLocator.getInstance().getBean("codeService");
		}
		return codeUtil;
	}
	public Map getCodeAndName(final String table,final String column1, final String column2){
		return codeService.getCodeAndName(table,column1,column2);
	}
	public Map getCodeNames(String table){
		return codeService.getCodeNames(table);
	}

}
